import org.apache.commons.codec.binary.Hex;
import java.io.FileInputStream;
import java.security.*;
import java.security.cert.Certificate;
import javax.crypto.Cipher;


public class TestP12 {

    //faa02a581b1d2c26462820324fc35e4e658c752d2a73f1796cf6a63ca746ee33
    public static void main(String[] args) throws Exception {
//        MessageDigest instance = MessageDigest.getInstance("SHA-256");
////        System.out.println(Hex.encodeHexString(instance.digest(getPrivateKey().getEncoded())));
////        System.out.println(Hex.encodeHexString(instance.digest(getCertificate().getEncoded())));
//        System.out.println(Hex.encodeHexString(getCertificate().getEncoded()));
        System.out.println(Hex.encodeHexString(getPublicKey().getEncoded()));
//        System.out.println(Hex.encodeHexString(getPrivateKey().getEncoded()));


        /**
         * 04cec7e74ba698d918ac986cce45d2b8cb124cf7e5e04c8e70546883cccd3849b625e227b5b63c8dabd935e51ba964caa10947aa7b50b20d2e47d07a271cd36fcd
         * faa02a581b1d2c26462820324fc35e4e658c752d2a73f1796cf6a63ca746ee33
         * 6cbf6904c0ccd88ca4f9453f06021ef2a937da6f5d63e67de2bad5fedb5ffca8343398f511cbbecdc20f4ec49bc0799aeb12298c37af9ceb183ca20822a0567b1b4dab633595e9e9fb9970683b06a565e4ba9b58b871df3f16b41665bf24fe4d02b36865bee0ecb212233fa617d78604e3220f757157a1591ea39820f40070c895e32068ab9b5c3f32e75389fb25c3242d10a1e9ce4b3851d431a1832abf8d3532dc29ba8737ffc62321aa058195142c05ee17b0f28acc4f9a1b5c6bec9b3cff1da8908585fbe4b29f7551e8e8bb27e8697ad20c5d460ddd8e7c37b2ef6f527b3a4d9ef3a8375c76f14baffe22ca5ff3eca67a16761c2ba3572d060a230c42bb
         */
//        StringBuffer sb = new StringBuffer();
//        for (byte b : getCertificate().getEncoded()) {
//            sb.append(String.format("%02X", b));
//        }
//        System.err.println(sb.toString());

//        byte[] bytes = Hex.decodeHex("6cbf6904c0ccd88ca4f9453f06021ef2a937da6f5d63e67de2bad5fedb5ffca8343398f511cbbecdc20f4ec49bc0799aeb12298c37af9ceb183ca20822a0567b1b4dab633595e9e9fb9970683b06a565e4ba9b58b871df3f16b41665bf24fe4d02b36865bee0ecb212233fa617d78604e3220f757157a1591ea39820f40070c895e32068ab9b5c3f32e75389fb25c3242d10a1e9ce4b3851d431a1832abf8d3532dc29ba8737ffc62321aa058195142c05ee17b0f28acc4f9a1b5c6bec9b3cff1da8908585fbe4b29f7551e8e8bb27e8697ad20c5d460ddd8e7c37b2ef6f527b3a4d9ef3a8375c76f14baffe22ca5ff3eca67a16761c2ba3572d060a230c42bb");
//
//        System.out.println(decryptByPrivateKey(bytes,getPublicKey()));

//        String hexStr = encryptToHex("黄飞", getPrivateKey());
//        System.out.println(hexStr);
//        String decrypt = decryptByHex(hexStr, getPublicKey());
//        System.out.println(decrypt);
//
//
//        String hexStr1 = encryptToHex("黄飞", getPublicKey());
//        System.out.println(hexStr);
//        String decrypt1 = decryptByHex(hexStr1, getPrivateKey());
//        System.out.println(decrypt1);




        String a = "6cbf6904c0ccd88ca4f9453f06021ef2a937da6f5d63e67de2bad5fedb5ffca8343398f511cbbecdc20f4ec49bc0799aeb12298c37af9ceb183ca20822a0567b1b4dab633595e9e9fb9970683b06a565e4ba9b58b871df3f16b41665bf24fe4d02b36865bee0ecb212233fa617d78604e3220f757157a1591ea39820f40070c895e32068ab9b5c3f32e75389fb25c3242d10a1e9ce4b3851d431a1832abf8d3532dc29ba8737ffc62321aa058195142c05ee17b0f28acc4f9a1b5c6bec9b3cff1da8908585fbe4b29f7551e8e8bb27e8697ad20c5d460ddd8e7c37b2ef6f527b3a4d9ef3a8375c76f14baffe22ca5ff3eca67a16761c2ba3572d060a230c42bb";
        String b="04de6a7f94544a464b524c13a5e359b488e723e93f43488b1b42159db977c1184cfc14963338c7c4028c06d786efff943e562b8aff1d28877c6cba2131efa5b499";

        String decrypt1 = decryptByHex(b, getPrivateKey());
        System.out.println(decrypt1);



    }




    public  static  PrivateKey getPrivateKey(){
        PrivateKey key = null;
        try {
            // 加载KeyStore
            char[] password = "123456".toCharArray(); // 更改为你的keystore密码
            KeyStore keyStore = KeyStore.getInstance("PKCS12");
            FileInputStream fis = new FileInputStream("C:\\Users\\huangjian01\\Desktop\\keystore.p12"); // 更改为你p12文件的路径
            keyStore.load(fis, password);
            fis.close();
            String alias = keyStore.aliases().nextElement(); // 假设只有一个条目
          //  System.out.println("Alias: " + alias);
            PrivateKey privateKey = (PrivateKey) keyStore.getKey(alias, password);
            key = privateKey;
        } catch (Exception e) {

        }
        return key;
    }


    public  static PublicKey getPublicKey(){
        PublicKey key = null;
        try {
            // 加载KeyStore
            char[] password = "123456".toCharArray(); // 更改为你的keystore密码
            KeyStore keyStore = KeyStore.getInstance("PKCS12");
            FileInputStream fis = new FileInputStream("C:\\Users\\huangjian01\\Desktop\\keystore.p12"); // 更改为你p12文件的路径
            keyStore.load(fis, password);
            fis.close();
            String alias = keyStore.aliases().nextElement(); // 假设只有一个条目
          //  System.out.println("Alias: " + alias);
            Certificate cert = keyStore.getCertificate(alias);
            PublicKey publicKey = cert.getPublicKey();
            key = publicKey;
        } catch (Exception e) {

        }
        return key;
    }




    public static Certificate getCertificate(){
        Certificate cert = null;
        try {
            // 加载KeyStore
            char[] password = "123456".toCharArray(); // 更改为你的keystore密码
            KeyStore keyStore = KeyStore.getInstance("PKCS12");
            FileInputStream fis = new FileInputStream("C:\\Users\\huangjian01\\Desktop\\keystore.p12"); // 更改为你p12文件的路径
            keyStore.load(fis, password);
            fis.close();
            String alias = keyStore.aliases().nextElement(); // 假设只有一个条目
            System.out.println("Alias: " + alias);
            cert = keyStore.getCertificate(alias);
        } catch (Exception e) {

        }
        return cert;
    }



    public static String encryptToHex(String text, Key key) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] encryptedBytes = cipher.doFinal(text.getBytes());
        return  Hex.encodeHexString(encryptedBytes);
    }


    public static String decryptByHex(String hexStr, Key key) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, key);
        byte[] decryptedBytes =  cipher.doFinal(Hex.decodeHex(hexStr));
                //cipher.doFinal(Base64.getDecoder().decode(encryptedText)); // 解码Base64字符串为字节数组
        return new String(decryptedBytes); // 将解密后的字节数组转换为字符串
    }


}
